/**
 * buffer转base64
 * @param buffer
 */
export const arrayBuffer2Base64 = (buffer: any): string => {
  const bytes = new Uint8Array(buffer)
  const binary = bytes.reduce((pre, cur) => {
    return pre + String.fromCharCode(cur)
  }, '')

  return `data:image/jpeg;base64,${window.btoa(binary)}`
}

/**
 * 文件流转base64
 * @param filestream
 */
export const filestream2Base64 = (filestream: string) => {
  return new Promise((resolve, reject) => {
    const blob = new Blob([filestream], { type: 'image/jpeg' })
    const fileReader = new FileReader()
    fileReader.readAsDataURL(blob)
    fileReader.onloadend = (e) => {
      resolve(e.target?.result)
    }
  })
}
